package book.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;

import book.biz.BookDaoBiz;
import book.entity.Book;

public class upAction extends BaseAction {
	private String isbn;
	private String name;
	private String author;
	private String publish;
	private int price;
	private int currcount;
	private File picture;
	private String pictureContentType;
	private String pictureFileName;
	private String savePath;
	
	public String execute(){
		String picturePath=getSavePath()+"\\"+getPictureFileName();
		doUpload(picture, picturePath);
		BookDaoBiz bookDaoBiz=new BookDaoBiz();
		Book book=new Book(isbn,name,author,publish,price,currcount,pictureFileName);
		Boolean flag=bookDaoBiz.BookAdd(book);
		if(flag){
			return SUCCESS;
		}else{
			return ERROR;
		}		
	}
	
	/**
	 * 上传文件
	 * @param file 文件
	 * @param uploadFilePath 路径
	 */
	public void doUpload(File file,String pictureFilePath){
		try {
			FileOutputStream fos=new FileOutputStream(pictureFilePath);
			FileInputStream fis=new FileInputStream(file);
			byte[] buffer=new byte[1024];
			int len=0;
			//循环读写
			while((len=fis.read(buffer))!=-1){
				fos.write(buffer, 0, len);
			}
			fis.close();
			fos.close();
		} catch (Exception e) {		
			e.printStackTrace();
		}
	}
	
	
	public upAction() {
	}

	

	public upAction(String isbn, String name, String author, String publish, int price, int currcount, File picture,
			String pictureContentType, String pictureFileName, String savePath) {
  
		this.isbn = isbn;
		this.name = name;
		this.author = author;
		this.publish = publish;
		this.price = price;
		this.currcount = currcount;
		this.picture = picture;
		this.pictureContentType = pictureContentType;
		this.pictureFileName = pictureFileName;
		this.savePath = savePath;
	}

	public String getPictureContentType() {
		return pictureContentType;
	}
	public void setPictureContentType(String pictureContentType) {
		this.pictureContentType = pictureContentType;
	}
	public String getPictureFileName() {
		return pictureFileName;
	}
	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public File getPicture() {
		return picture;
	}

	public void setPicture(File picture) {
		this.picture = picture;
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPublish() {
		return publish;
	}

	public void setPublish(String publish) {
		this.publish = publish;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getCurrcount() {
		return currcount;
	}

	public void setCurrcount(int currcount) {
		this.currcount = currcount;
	}



	
	
}
